app.controller('addressController' ,function($scope,$controller,addressService){
    $controller("commonController",{$scope:$scope});
    //删除地址
    $scope.delete=function (id) {
        addressService.delete(id).success(
            function (response) {
                alert(response.message);
                //刷新页面
                location.reload();
            }
        );
    }
    //添加地址
    $scope.dpAddress={"provinceId":"","cityId":""}
    $scope.save=function(){
        var serviceObject;
        if ($scope.dpAddress.id == null){
            serviceObject=addressService.add($scope.dpAddress);
        }else{
            serviceObject=addressService.update($scope.dpAddress);
        }
        serviceObject.success(
            function (response) {
                alert(response.message);
                //刷新页面
                location.reload();
            }
        );
    }
    //查询地址详情
    $scope.findOne=function (id) {
        addressService.findOne(id).success(
            function (response) {
                $scope.dpAddress=response;
            }
        );
    }
    //查询所有
    $scope.findAll=function () {
        addressService.findAll().success(
            function (response) {
                $scope.addressList=response;
            }
        )
    }
    //查询省级地址
    $scope.findProvinces=function () {
        addressService.findProvinces().success(
            function (response) {
                $scope.proviceList=response;
            }
        )
    }
    //当省级地址发生变化后，市级地址也发生响应变化
    $scope.$watch("dpAddress.provinceId",function (newValue,oldValue) {
        addressService.findCity(newValue).success(
            function (response) {
                $scope.citiesList=response;
            }
        )
    });
    //当市级地址发生变化后，查询县级地址
    $scope.$watch("dpAddress.cityId",function (newValue,oldValue) {
        addressService.findArea(newValue).success(
            function (response) {
                $scope.areasList=response;
            }
        )
    });
    //设置默认地址
    $scope.isDefault=function (id) {
        addressService.isDefault(id).success(
            function (response) {
                alert(response.message);
                //刷新页面
                location.reload();
            }
        );
    }
});